package com.ops.chirpit;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;



import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;

public class DialogListAdapter implements ListAdapter {
	
	private JSONArray mObjectList;
	private int mListType;
	public LayoutInflater mInflater;
	private Context mContext;
	private OnClickListener cb1OCL;

	public DialogListAdapter(Context context,
		JSONArray objectList, int id) {
		mObjectList = objectList;
		mContext = context;
		mInflater = LayoutInflater.from(context);
		mListType = id;
	}
	
	public static abstract class OnClickListener implements
    View.OnClickListener {

	private ViewHolder mViewHolder;
	
	/**
	* @param holder The holder of the clickable item
	*/
	public OnClickListener(ViewHolder holder) {
	    mViewHolder = holder;
	}
	
	// delegates the click event
	public void onClick(View v) {
	    onClick(v, mViewHolder);
	}
	
	/**
	* Implement your click behavior here
	* @param v  The clicked view.
	*/
	public abstract void onClick(View v, ViewHolder viewHolder);
	};

	public int getCount() {
		return mObjectList.length();
	}

	public Object getItem(int position) {
		try {
			return mObjectList.get(position);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	public long getItemId(int position) {
		return(position);
	}

	public int getItemViewType(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		  if (convertView == null) {
		   convertView = mInflater.inflate(R.layout.dialog_list_item, null);
		   holder = createHolder(convertView);

		   convertView.setTag(holder);
		  } else {
		   holder = (ViewHolder) convertView.getTag();
		  }
		  
		  try {
			  holder.position = position;
			  switch(mListType){
			  case ChirpItActivity.PICK_ACCOUNTS:			  
				  JSONObject item = (JSONObject) getItem(position);
				  int drawable = AccountType.getDrawable(item.getInt("network"));
				  holder.cBox1.setVisibility(View.VISIBLE);
				  holder.cBox1.setChecked(ChirpItActivity.tCheckedAccounts[position]);
			      holder.icon1.setImageResource(drawable);
				  holder.title.setText(item.getString("name"));
				  holder.line1.setVisibility(View.GONE);
				  holder.line2.setVisibility(View.GONE);
				  holder.icon2.setVisibility(View.GONE);
				  OnClickListener cb1OCL = new OnClickListener(holder) {
						public void onClick(View v, ViewHolder holder) {
							ChirpItActivity.tCheckedAccounts[holder.position] = !ChirpItActivity.tCheckedAccounts[holder.position];
						}
			      };
				  holder.cBox1.setOnClickListener(cb1OCL);
				  OnClickListener deleteOCL = new OnClickListener(holder) {
						public void onClick(View v, ViewHolder holder) {
							JSONObject object = (JSONObject) getItem(holder.position);
							try {
								ChirpItActivity.deleteObjects.add(object.getString("id"));
								((View) v.getParent()).setVisibility(View.GONE);
							} catch (JSONException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
			      	};
				  holder.icon3.setOnClickListener(deleteOCL);
				  break;
			  case ChirpItActivity.MEDIA:
				  String[] image = (String[]) getItem(position);
				  Drawable d = Drawable.createFromPath(image[0]);
				  String title = image[0].substring(image[0].lastIndexOf("/")+1);
				  holder.icon1.setImageDrawable(d);
			      holder.icon1.setAdjustViewBounds(true);
			      holder.icon1.setMaxHeight(100);
			      holder.icon1.setMaxWidth(100);
				  holder.cBox1.setVisibility(View.GONE);
				  holder.title.setText(title);
				  holder.line1.setVisibility(View.GONE);
				  holder.line2.setVisibility(View.GONE);
				  holder.icon2.setVisibility(View.GONE);
				  OnClickListener deleteOCL1 = new OnClickListener(holder) {
						public void onClick(View v, ViewHolder holder) {
							String[] object = (String[]) getItem(holder.position);
							ChirpItActivity.deleteObjects.add(object[0]);
							((View) v.getParent()).setVisibility(View.GONE);
						}
			      	};
				  holder.icon3.setOnClickListener(deleteOCL1);
				  break;
			  }
			} catch (Exception e) {
			  // TODO Auto-generated catch block
			  e.printStackTrace();
			}
		  return convertView;
	}
	 protected ViewHolder createHolder(View convertView){	 
		 ViewHolder holder = new ViewHolder();
		   holder.cBox1 = (CheckBox) convertView.findViewById(R.id.dli_checkBox1);
	       holder.icon1 = (ImageView) convertView.findViewById(R.id.dli_icon1);
	       holder.title = (TextView) convertView.findViewById(R.id.dli_title);
	       holder.line1 = (TextView) convertView.findViewById(R.id.dli_line1);
	       holder.line2 = (TextView) convertView.findViewById(R.id.dli_line2);
	       holder.icon2 = (ImageView) convertView.findViewById(R.id.dli_icon2);
	       holder.icon3 = (ImageView) convertView.findViewById(R.id.dli_icon3);
		return holder;
		 
	 }

     /**
      * Binds the data from user's object to the holder
      * @param h  The holder that shall represent the data object.
      */
     protected void bindHolder(ViewHolder h){
    	 
     }

	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return 1;
	}

	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean isEmpty() {
		// TODO Auto-generated method stub
		return false;
	}
	
	public void notifyDataSetChanged() {
		
	}

	public void registerDataSetObserver(DataSetObserver observer) {
		// TODO Auto-generated method stub

	}

	public void unregisterDataSetObserver(DataSetObserver observer) {
		// TODO Auto-generated method stub

	}

	public boolean areAllItemsEnabled() {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean isEnabled(int arg0) {
		// TODO Auto-generated method stub
		return false;
	}
	
	static class ViewHolder {
		int position;
		CheckBox cBox1;
        ImageView icon1;
        TextView title;
        TextView line1;
        TextView line2;
        ImageView icon2;
        ImageView icon3;
 }

}
